#//////////////////////////////////////////////////////////////////////////////
#
#	Makefile for Lab Fisica example DAQ code
#
#	A.Lusiani, April 2003
#
#//////////////////////////////////////////////////////////////////////////////

# --- ARCH is set by Root according to the current computer architecture
ARCH          = $(shell root-config --arch)

# --- set Root related C/C++ flags
ROOTCFLAGS   := $(shell root-config --cflags)
# --- set Root related libraries
ROOTLIBS     := $(shell root-config --libs)
# --- set Root related graphical libraries
ROOTGLIBS    := $(shell root-config --glibs)
# --- RooFit libraries
ROOFITLIBS   	:= -lRooFitCore -lRooFitModels

# --- libraries for programs using Comedi
COMEDILIBS	= -lcomedi -lm

# --- Root configuration for Linux: BEGIN
CC            = gcc
CXX           = g++
CXXFLAGS      = -O -Wall -fPIC
LD            = g++
LDFLAGS       = -O
SOFLAGS       = -shared
# --- Root configuration for Linux: END

# --- standard C compilation flags for Linux
CFLAGS = -O -Wall -fPIC -Wstrict-prototypes

# --- search for include files also on current directory
CFLAGS += -I .
CXXFLAGS += -I .

CXXFLAGS     	+= $(ROOTCFLAGS)

LIBS          	= $(ROOTLIBS) $(SYSLIBS)
GLIBS         	= $(ROOTGLIBS) $(SYSLIBS)
RFLIBS		= $(ROOFITLIBS) $(ROOTGLIBS) -lHtml -lMinuit $(SYSLIBS)

ifdef ROOTSYS
# ERROR := $(shell echo >&2 "ROOTSYS defined: Root includes in RS/include")
ROOTINCL	:= $(ROOTSYS)/include
else
# ERROR := $(shell echo >&2 "ROOTSYS not defined: Root includes in /usr")
ROOTINCL	:= /usr/include/root
endif

ifdef DEBUG
CFLAGS     	+= -g
CXXFLAGS     	+= -g
LDFLAGS      	+= -g
endif

#//////////////////////////////////////////////////////////////////////////////
#
#	reconfigure compiler for a specific host
#

ifeq (bbr-pi06,$(HOST))
	override CPP=gcc-2.95.3 -E
	override CC=gcc-2.95.3
	override CXX=g++-2.95.3
endif

#//////////////////////////////////////////////////////////////////////////////
#
#	Rules
#

# --- needed to generate dependencies
export MFLAG	:= -MM

%.d: %.c
	@ echo "Making $@"
	@ $(SHELL) -ec '$(CPP) $(MFLAG) $(CPPFLAGS) $< | \
	  sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' | \
	  perl -pe '\''undef $/; s:$(ROOTINCL)/\S*\s*::g; s/[\\\s]*$$//'\'' \
	  > $@'

%.d: %.cc
	@ echo "Making $@"
	@ $(SHELL) -ec '$(CPP) $(MFLAG) $(CPPFLAGS) $(ROOTCFLAGS) $< | \
	  sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' | \
	  perl -pe '\''undef $/; s:$(ROOTINCL)/\S*\s*::g; s/[\\\s]*$$//'\'' \
	  > $@'

#
# general rule to generate Root dictionaries 
#

%Dict.cc: %.h %LinkDef.h
	rootcint -f $@ -c $^

# /////////////////////////////////////////////////////////////////////////////
#
#	first target: help message
#

.PHONY:		help all clean cleanall release web

help:
	@echo "type \"make all\" to build everything"

#//////////////////////////////////////////////////////////////////////////////
#
# 	Dependency files
# 	- automatically generate .d files and include them
# 

# --- files for which dependency files must not be generated
DEPEXFILES := finFit.cc

CXXSOURCES := $(filter-out $(DEPEXFILES), $(wildcard *.cc))
CSOURCES := $(filter-out $(DEPEXFILES), $(wildcard *.c))

ifneq ($(CXXSOURCES:.cc=.d),)
-include $(CXXSOURCES:.cc=.d)
endif

ifneq ($(CSOURCES:.c=.d),)
-include $(CSOURCES:.c=.d)
endif

# /////////////////////////////////////////////////////////////////////////////
#
#	C executables linking Comedi
#

CBINS_1		= 

$(CBINS_1): % : %.o
	$(CC) -o $@ $^ $(LIBS) $(COMEDILIBS)

# /////////////////////////////////////////////////////////////////////////////
#
# 	C++ executables linking Comedi, Root and labFisDaq.o
#

CXXBINS_1	= 

$(CXXBINS_1): % : %.o labFisDaq.o
	$(CXX) -o $@ $^ $(LIBS) $(COMEDILIBS)

# /////////////////////////////////////////////////////////////////////////////
#
# 	C++ executables
#	- linking Comedi, Root, labFisDaq.o
# 	- using an event class for which a Root dictionary is needed
#

CXXBINS_2	=

$(CXXBINS_2): % : %.o labFisPendEv.o labFisPendEvDict.o labFisDaq.o 
	$(CXX) -o $@ $^ $(LIBS) $(COMEDILIBS)

# /////////////////////////////////////////////////////////////////////////////
#
# 	C++ executables
#	- linking Comedi, Root, RooFit
#

CXXBINS_3	= finFitSA

$(CXXBINS_3): % : %.o
	$(CXX) -o $@ $^ $(RFLIBS) $(COMEDILIBS)

# /////////////////////////////////////////////////////////////////////////////
#
#	targets
#

# --- all binaries
ALLBINS		= $(CBINS_1) $(CXXBINS_1) $(CXXBINS_2) $(CXXBINS_3)

# --- build everything
all:		$(ALLBINS)
	@echo "all targets done"

# --- clean up
clean:
	-rm -f .#*
	-rm -f *.o *.so $(ALLBINS)
	-rm -f *Dict.* 

# --- clean up, including dependency files
cleanall:	clean
	-rm -f *.d

# --- archive current snapshot into .tar.gz file
release:	clean
	tar -C .. --exclude '*~' \
          --exclude 'CVS/*' --exclude '.cvsignore' \
          --exclude 'labFis/*.d' \
          --exclude 'labFis/*.root' \
          --exclude 'labFis/*.ps' --exclude 'labFis/*.eps' \
          --exclude 'labFis/*.gif' --exclude 'labFis/*.png' \
	  -cvzf ../labFis-`date '+%Y-%m-%d'`.tgz labFis finData

# --- update "examples" directory on web site
web:		clean
	rsync -essh -avCz . \
          --exclude '.cvsignore' \
          --exclude 'labFis/*.d' \
          --exclude 'labFis/*.root' \
          --exclude 'labFis/*.ps' --exclude 'labFis/*.eps' \
          --exclude 'labFis/*.gif' --exclude 'labFis/*.png' \
	  cibs:/home/hep/lusiani/LabFis/wiki/data/examples/.
